package com.fan.play.media.capture

import com.fan.play.media.capture.audio.AudioRecordCapture

/**
 * 音频的采集
 *
 * @constructor
 *
 * @param sampleRate 采样率
 * @param channelNum 采样通道数
 * @param sampleBit 采样位数
 * @param nativeRef C++对象的指针
 */
class AudioCapture(
    sampleRate: Int, channelNum: Int, sampleBit: Int, nativeRef: Long
) {

    private var mNativeRef: Long

    //具体的音频采集对象
    private lateinit var mCapture: ICapture

    private var mAudioCallback = object : IAudioCallback {
        override fun onAudioFrame(audioBuf: ByteArray, rawType: Int) {
            onAudioFrame(audioBuf, rawType, nativeRef)
        }
    }

    init {
        mNativeRef = nativeRef
        mCapture = AudioRecordCapture(sampleRate, channelNum, sampleBit, mAudioCallback)
    }

    fun start(): Boolean {
        return mCapture.start()
    }

    fun stop() {
        mCapture.stop()
    }

    /**
     * 一帧音频传递到C++层
     *
     * @param audioBuf 音频数据
     * @param rawType 音频原始数据类型
     * @param nativeRef C++对象的指针
     */
    private external fun onAudioFrame(audioBuf: ByteArray, rawType: Int, nativeRef: Long)
}